package com.sinothk.gpserver.modules.texasPoker.helper;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 编写 Liang Y.T.
 * 时间 2021/6/29 14:46
 */
public class TexasPokerHelper {

    /**
     * 发给每个人两张
     *
     * @param allArr    洗牌后扑克
     * @param playerNum 参加人数
     * @return
     */
    public static TexasPokerBean sendPersonCard(Poker[] allArr, int playerNum) {
        TexasPokerBean texasPokerBean = new TexasPokerBean();
        ArrayList<Poker> allList = new ArrayList<>(Arrays.asList(allArr));
        int startAll = 2 * playerNum;

        // 分配牌
        ArrayList<Poker> startList = new ArrayList<>(allList.subList(0, startAll));
        texasPokerBean.setStartPokerList(startList);
        // 每人得到的牌
        ArrayList<ArrayList<Poker>> userPokerList = ListUtils.averageAssign(startList, 2);
        texasPokerBean.setUserPokerList(userPokerList);

        // 发后剩余牌
        List<Poker> endPokerList = allList.subList(startAll, allList.size());
        texasPokerBean.setRemainPokerList(new ArrayList<>(endPokerList));

        return texasPokerBean;
    }

    /**
     * 发三张公共牌
     *
     * @param texasPoker
     * @return
     */
    public static TexasPokerBean sendPubCard(TexasPokerBean texasPoker) {
        // 三张公共牌
        List<Poker> publicPokerList = texasPoker.getRemainPokerList().subList(0, 3);
        texasPoker.setPublicPokerList(new ArrayList<>(publicPokerList));

        // 设置新的剩余牌
        List<Poker> newRemainPokerList = texasPoker.getRemainPokerList().subList(3, texasPoker.getRemainPokerList().size());
        texasPoker.setRemainPokerList(new ArrayList<>(newRemainPokerList));
        return texasPoker;
    }

    /**
     * 发一次牌
     *
     * @param texasPoker
     * @return
     */
    public static TexasPokerBean sendOneCard(TexasPokerBean texasPoker) {
        // 取出余牌第一张牌放到公共牌中
        // 获取现有的公共牌集合
        ArrayList<Poker> remainPokerList = texasPoker.getRemainPokerList();// 取出余牌
        texasPoker.getPublicPokerList().add(remainPokerList.get(0)); // 取一张放入公共牌

        // 设置新的剩余牌
        List<Poker> remainPokerListNew = remainPokerList.subList(1, remainPokerList.size());
        texasPoker.setRemainPokerList(new ArrayList<>(remainPokerListNew));
        return texasPoker;
    }


    public static ArrayList<Poker> getMax5In7(ArrayList<Poker> pokers) {
        return null;
    }

    public static TexasPokerBean sendPubRemainingCards(TexasPokerBean texasPoker) {
        // 取出余牌,从中取出剩余公共牌，放到公共牌集合中
        // 获取现有的公共牌集合
        ArrayList<Poker> remainPokerList = texasPoker.getRemainPokerList();// 取出余牌
        int remainingNum = 5 - texasPoker.getPublicPokerList().size();

        List<Poker> addList = remainPokerList.subList(0, remainingNum);
        texasPoker.getPublicPokerList().addAll(addList); // 放入公共牌

        // 设置新的剩余牌
        List<Poker> remainPokerListNew = remainPokerList.subList(remainingNum, remainPokerList.size());
        texasPoker.setRemainPokerList(new ArrayList<>(remainPokerListNew));
        return texasPoker;
    }


//    /**
//     * 获取德州扑克开始发牌
//     *
//     * @param allArr    洗牌后扑克
//     * @param playerNum 参加人数
//     * @return
//     */
//    public static TexasPokerBean getStartPoker(Poker[] allArr, int playerNum) {
//        TexasPokerBean texasPokerBean = new TexasPokerBean();
//        ArrayList<Poker> allList = new ArrayList<>(Arrays.asList(allArr));//Arrays.stream(allArr).collect(Collectors.toList());
//        int startAll = 2 * playerNum;
//
//        // 分配牌
//        ArrayList<Poker> startList = new ArrayList<>();
//        startList.addAll(allList.subList(0, startAll));
//        texasPokerBean.setStartPokerList(startList);
//        // 每人得到的牌
//        texasPokerBean.setUserPokerList(ListUtils.averageAssign(startList, 2));
//
//        // 三张公共牌
//        int newIndex = startAll + 3;
//        texasPokerBean.setPublicPokerList(new ArrayList<>(allList.subList(startAll, newIndex)));
//
//        // 发后剩余牌
//        List<Poker> endPokerList = allList.subList(newIndex, allList.size());
//        texasPokerBean.setEndPokerList(new ArrayList<>(endPokerList));
//
//        // 设置参与人数
//        texasPokerBean.setPlayerNum(playerNum);
//
//        return texasPokerBean;
//    }
//    /**
//     * 发一次牌
//     *
//     * @param texasPokerBean
//     * @param num
//     */
//    public static void runTimes(TexasPokerBean texasPokerBean, int num) {
//        List<Poker> endPokerList = texasPokerBean.getRemainPokerList();
//        int size = endPokerList.size();
//
//        texasPokerBean.getPublicPokerList().addAll(endPokerList.subList(0, num));
//        // 返回发后余牌
////        texasPokerBean.setR(new ArrayList<>(endPokerList.subList(num + 1, size)));
//    }
    //    /**
//     * 获取德州扑克开始发牌
//     *
//     * @param allArr    洗牌后扑克
//     * @param playerNum 参加人数
//     * @return
//     */
//    public static TexasPokerBean getStartPoker(Poker[] allArr, int playerNum) {
//        TexasPokerBean texasPokerBean = new TexasPokerBean();
//        ArrayList<Poker> allList = new ArrayList<>(Arrays.asList(allArr));//Arrays.stream(allArr).collect(Collectors.toList());
//        int startAll = 2 * playerNum;
//
//        // 分配牌
//        ArrayList<Poker> startList = new ArrayList<>();
//        startList.addAll(allList.subList(0, startAll));
//        texasPokerBean.setStartPokerList(startList);
//        // 每人得到的牌
//        texasPokerBean.setUserPokerList(ListUtils.averageAssign(startList, 2));
//
//        // 三张公共牌
//        int newIndex = startAll + 3;
//        texasPokerBean.setPublicPokerList(new ArrayList<>(allList.subList(startAll, newIndex)));
//
//        // 发后剩余牌
//        List<Poker> endPokerList = allList.subList(newIndex, allList.size());
//        texasPokerBean.setEndPokerList(new ArrayList<>(endPokerList));
//
//        // 设置参与人数
//        texasPokerBean.setPlayerNum(playerNum);
//
//        return texasPokerBean;
//    }

    /**
     * 只发一张给每个玩家
     *
     * @param texasPoker
     * @return
     */
    public static String sendOneCardToPrePlayer(TexasPokerBean texasPoker) {
        // 取出余牌第一张牌放到公共牌中

        // 取出余牌
        ArrayList<Poker> remainPokerList = texasPoker.getRemainPokerList();//

        // 设置新的剩余牌
        List<Poker> remainPokerListNew = remainPokerList.subList(1, remainPokerList.size());
        texasPoker.setRemainPokerList(new ArrayList<>(remainPokerListNew));

        // 取一张放入公共牌
        return remainPokerList.get(0).toString();
    }
}
